/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.configurator.ui;

import dale2507.gates.configurator.ui.components.BlockSelector;
import dale2507.gates.data.Settings;
import dale2507.gates.data.Settings.GateConnectionSettings;
import dale2507.gates.gate.logic.IActivatorLogic;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import org.bukkit.Material;

/**
 *
 * @author Dale
 */
class ConnectionSettings extends PropertiesPage {
    
    private JComboBox activatorLogic = new JComboBox(new String[]{"QuickDial", "FailBeforeDial", "FailAfterDial"});
    private JSpinner connectionTime = new JSpinner();
    private JSpinner autoOff = new JSpinner();
    private JSpinner chevronDelay = new JSpinner();
    private JCheckBox checkAmount = new JCheckBox();
    private JCheckBox vortex = new JCheckBox();
    private JCheckBox vehicles = new JCheckBox();
    private JCheckBox killWrongWay = new JCheckBox();
    private BlockSelector irisMaterial = new BlockSelector();
    
    ConnectionSettings() {
        super();
        
        this.addLabelComponentPair("Activator Logic", activatorLogic, "The logic class to be used when dialling a gate.");
        
        SpinnerNumberModel snm = new SpinnerNumberModel(38, 10, 120, 10);
        connectionTime.setModel(snm);
        connectionTime.setEditor(new JSpinner.NumberEditor(connectionTime));
        this.addLabelComponentPair("Connection Time", connectionTime, "The maximum amount of time which a connection can stay active in seconds (Unless it is an indefinite connection gate).");
        
        snm = new SpinnerNumberModel(10, 0, 30, 5);
        autoOff.setModel(snm);
        autoOff.setEditor(new JSpinner.NumberEditor(autoOff));
        this.addLabelComponentPair("Auto Off", autoOff, "Delay before gate is deactivated due to inactivity");
        
        snm = new SpinnerNumberModel(15, 0, 20, 1);
        chevronDelay.setModel(snm);
        chevronDelay.setEditor(new JSpinner.NumberEditor(chevronDelay));
        this.addLabelComponentPair("Chevron Delay", chevronDelay, "Time between each cevron torch appearing. A value of 20 is equal to 1 second.");
        this.addLabelComponentPair("Check Quantity", checkAmount, "Use the quantity of items in a stack as an extra address parameter.");
        this.addLabelComponentPair("Enable vortex", vortex, "Enables the vortex which is emitted from gates when a connection is established.");
        this.addLabelComponentPair("Allow vehicles", vehicles, "Enables transporting vehicles through gates.");
        this.addLabelComponentPair("Kill On Wrong Way", killWrongWay, "Kills any player who attempts to enter an incoming connection.");
        this.addLabelComponentPair("Iris Material", irisMaterial, "Material which the iris is made of.");
        
        
        if (Configurator.isLive()) {
            liveLoad();
        } else {
            // TODO 
        }
    }

    private void liveLoad() {
        GateConnectionSettings s = Settings.getInstance().connection;
        activatorLogic.getModel().setSelectedItem(s.activatorLogicClass().getSimpleName());
        connectionTime.getModel().setValue(s.maximumConnectionTime() / 20L);
        autoOff.getModel().setValue(s.autoOffTime() / 20L);
        chevronDelay.getModel().setValue(s.chevronDelay());
        checkAmount.setSelected(s.checkAmount());
        vortex.setSelected(s.enableVortex());
        vehicles.setSelected(s.allowVehicles());
        killWrongWay.setSelected(s.killOnWrongWay());
        irisMaterial.setSelectedItem(s.irisMaterial());
    }
    
    @Override
    void liveSave() {
        GateConnectionSettings s = Settings.getInstance().connection;
        try {
            s.setActivatorLogicClass((Class<? extends IActivatorLogic>) Class.forName("dale2507.gates.gate.logic." + String.valueOf(activatorLogic.getSelectedItem())));
        } catch (ClassNotFoundException | ClassCastException ex) {
            Logger.getLogger(ConnectionSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        s.setConnectionTime(((int) connectionTime.getValue()) * 20L);
        s.setAutoOff(((int) autoOff.getValue()) * 20L);
        s.setChevronDelay((long) chevronDelay.getValue());
        s.setCheckAmount(checkAmount.isSelected());
        s.setEnableVortex(vortex.isSelected());
        s.setAllowVehicles(vehicles.isSelected());
        s.setKillOnWrongWay(killWrongWay.isSelected());
        s.setIrisMaterial((Material) irisMaterial.getSelectedItem());
    }

    @Override
    String toYaml() {
        return "# Settings for gate connections\r\n" + 
            "gate_connection:\r\n" + 
            "  \r\n" + 
            "  # Maximum connection time in seconds\r\n" + 
            "  # Default: 38\r\n" + 
            "  connection_time: " + String.valueOf(connectionTime.getModel().getValue()) + "\r\n" + 
            "  \r\n" + 
            "  # Auto off time if no traveller goes through gate\r\n" + 
            "  # Default: 10\r\n" + 
            "  auto_off_time: " + String.valueOf(autoOff.getModel().getValue()) + "\r\n" + 
            "  \r\n" + 
            "  # Delay between dialing each chevron (address item)\r\n" + 
            "  # Default: 15\r\n" + 
            "  chevron_delay: " + String.valueOf(chevronDelay.getModel().getValue()) + "\r\n" + 
            "  \r\n" +
            "  # Use item stack amount as address parameter\r\n" +
            "  # i.e. number of items are specific to each address\r\n" +
            "  # Default: false\r\n" +
            "  cheack_amount: " + checkAmount.isSelected() + "\r\n" +
            "  \r\n" + 
            "  # Enable vortex\r\n" + 
            "  # Default: true\r\n" + 
            "  vortex: " + vortex.isSelected() + "\r\n" + 
            "  \r\n" + 
            "  # Allow vehicles\r\n" + 
            "  # Default: true\r\n" + 
            "  allow_vehicles: " + vehicles.isSelected() + "\r\n" + 
            "  \r\n" + 
            "  # Kill players who try to enter a connection the wrong way\r\n" + 
            "  kill_wrong_way: " + killWrongWay.isSelected() + "\r\n" + 
            "  \r\n" + 
            "  # Iris Material\r\n" + 
            "  # Default: 42 (Iron Block)\r\n" + 
            "  iris_material: " + irisMaterial.getTypeId() + "\r\n";
    }
    
}
